import java.util.*; class Rand { private Random rng = new Random(); public Object[] range(int lower, int upper, int step) { double length = (upper - lower) / step; int len = (int)length; Object[] list = new Object[len]; for (int counter = 0; counter < len; counter += step) { list[counter] = lower + step*counter; } return list; } public void shuffle(Object[] list) { int length = list.length; for (int i=0; i < length; i++) { int index1 = rng.nextInt(length); int index2 = rng.nextInt(length); Object temp = list[index1]; list[index1] = list[index2]; list[index2] = temp; } } } class Node { public Object cargo; public Node next; public Node(Object cg, Node nt) { cargo = cg; next = nt; } } class Linked_List { private int length = 0; public Node head; public Node last; public boolean is_empty() { return length == 0; } public void append(Object cargo) { if (length <= 0) { Node new_head = new Node(cargo, null); head = new_head; last = new_head; length += 1; } else { Node new_last = new Node(cargo, null); last.next = new_last; last = new_last; length += 1; } } public void remove(Object target) { if (length > 0) { Node item = head; if (target.equals(head.cargo)) { head = head.next; length -= 1; // %s will be substituted by following string arguments, and %n represents a new line System.out.printf("Successfully remove '%s' from the list%n", target.toString()); } while (item.next != null) { if (target.equals(item.next.cargo)) { item.next = item.next.next; length -= 1; System.out.printf("Successfully remove '%s' from the list%n", target.toString()); break; } item = item.next; } } } public void print_all() { System.out.print("["); if (length > 0) { Node item = head; while (true) { if (item.next == null) { System.out.printf("%s", item.cargo.toString()); break; } System.out.printf("%s, ", item.cargo.toString()); item = item.next; } } System.out.println("]"); } } public class Test_Java { static void print_list(Object[] list) { System.out.print("["); if (list[0]==null){ System.out.println("]"); return; } System.out.print(list[0].toString()); for (int i = 1; i < list.length; i++) { if (list[i]==null){ System.out.println("]"); return; } System.out.printf(", %s", list[i].toString()); } System.out.println("]"); } public static void main(String[] args) { Linked_List linklist1 = new Linked_List(); Rand rand1 = new Rand(); Object[] list = rand1.range(0, 10, 1); for (Object i : list) { linklist1.append(i); } linklist1.print_all(); rand1.shuffle(list); print_list(list); for (Object i : list) { linklist1.remove(i); } linklist1.print_all(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Abstract_Data_Structures { class Program { class Rand { private Random rng = new Random(); public object[] range(int lower, int upper, int step=1) { double length = (upper - lower) / step; int len = (int)length; object[] list = new object[len]; for (int counter = 0; counter < len; counter += step) { list[counter] = lower + step*counter; } return list; } public void shuffle(object[] list) { int length = list.Length; for (int i=0; i < length; i++) { int index1 = rng.Next(0, length); int index2 = rng.Next(0, length); object temp = list[index1]; list[index1] = list[index2]; list[index2] = temp; } } } class Node { public object cargo; public Node next; public Node(object cg, Node nt) { cargo = cg; next = nt; } } class Linked_List { private int length = 0; public Node head; public Node last; public bool is_empty() { return length == 0; } public void append(object cargo) { if (length <= 0) { Node new_head = new Node(cargo, null); head = new_head; last = new_head; length += 1; } else { Node new_last = new Node(cargo, null); last.next = new_last; last = new_last; length += 1; } } public void remove(object target) { if (length > 0) { Node item = head; if (target == head.cargo) { head = head.next; length -= 1; Console.WriteLine("Successfully remove '{0}' from the list", target.ToString()); } while (item.next != null) { if (target == item.next.cargo) { item.next = item.next.next; length -= 1; Console.WriteLine("Successfully remove '{0}' from the list", target.ToString()); break; } item = item.next; } } } public void print_all() { Console.Write("["); if (length > 0) { Node item = head; while (true) { if (item.next == null) { Console.Write("{0}", item.cargo.ToString()); break; } Console.Write("{0}, ", item.cargo.ToString()); item = item.next; } } Console.WriteLine("]"); } } static void print_list(object[] list) { Console.Write("["); for (int i = 0; i < list.Length - 1; i++) { Console.Write("{0}, ", list[i].ToString()); } Console.Write("{0}]", list[list.Length - 1].ToString()); Console.WriteLine(); } static void Main(string[] args) { Linked_List linklist1 = new Linked_List(); Rand rand1 = new Rand(); object[] list = rand1.range(0, 10); foreach (object i in list) { linklist1.append(i); } linklist1.print_all(); rand1.shuffle(list); print_list(list); foreach (object i in list) { linklist1.remove(i); } linklist1.print_all(); Console.ReadKey(); } } }
You need to enable Javascript in your browser to edit pages.
help on how to format text
Notice how similar they are……
Java
C#